package com.jz.banner.screen.image

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.jz.banner.screen.utils.CommUtils
import com.jz.banner.screen.utils.Urls

/**
 * 创建者     Noel
 * 创建时间   2017/5/9 10:48
 * 描述	     图片管理
 */
object ImageManger {
    fun setBgImage(context: Context, url: String?, iv: ImageView) {
        var options = RequestOptions().priority(Priority.HIGH) //优先级
            //.error(R.drawable.ic_empty_food_white) //加载失败显示图片
            //.placeholder(R.drawable.ic_empty_food_white)
            .transform(MultiTransformation(CenterCrop()/*, GlideRoundTransform(raid)*/))
        Glide.with(context)
            .load(formatUrl(url))
            .apply(options)
            .into(iv)
    }

    fun setFitCenterImg(context: Context, url: String?, iv: ImageView, w: Int = 0, h: Int = 0) {
        try {
            var options = RequestOptions().priority(Priority.HIGH) //优先级
                .transform(MultiTransformation(FitCenter()))
                .error(w) //加载失败显示图片
            //.placeholder(h)
            //.override(w, h)
            Glide.with(context)
                .load(formatUrl(url))
                .apply(options)
                .into(iv)
        } catch (e: Exception) {
            CommUtils.println("ImageManager->", "setFitCenterImg error:$e")
        }
    }

    fun setFitCenterImg1(context: Context, url: String?, iv: ImageView, duration: Int = 300, errorResInt: Int = 0, placeResInt: Int = 0) {
        var options = RequestOptions().priority(Priority.HIGH) //优先级
            .error(errorResInt) //加载失败显示图片
            //.placeholder(placeResInt)
            .transform(MultiTransformation(FitCenter()/*, GlideRoundTransform(raid)*/))
        CommUtils.println("ImageManger:setFitCenterImg1==", /*Urls.BaseImgUrl +*/ "${formatUrl(url)}")
        Glide.with(context)
            .load(formatUrl(url))
            //.transition(DrawableTransitionOptions.withCrossFade(duration))
            .apply(options)
            .into(iv)
    }


    //显示圆角图
    fun setCircleImg(context: Context, url: String?, iv: ImageView, duration: Int = 300, errorResInt: Int = 0, placeResInt: Int = 0) {
        CommUtils.println("setCircleImg:", /*Urls.BaseImgUrl +*/ "${formatUrl(url)}")
        Glide.with(context)
            .load(formatUrl(url))
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .transition(DrawableTransitionOptions.withCrossFade(duration))
            .into(iv)
    }

    fun setCenterCropImg(context: Context, url: String?, iv: ImageView, duration: Int = 300, errorResInt: Int = 0, placeResInt: Int = 0) {
        try {
            var options = RequestOptions().priority(Priority.HIGH) //优先级
                .error(errorResInt) //加载失败显示图片
                .placeholder(placeResInt)
                .transform(MultiTransformation(CenterCrop()/*, GlideRoundTransform(raid)*/))
            CommUtils.println("ImageManger:", "${formatUrl(url)}}")
            Glide.with(context)
                .load(formatUrl(url))
                .apply(options)
                .transition(DrawableTransitionOptions.withCrossFade(duration))
                .into(iv)
        } catch (e: Exception) {
            CommUtils.println("ImageManager->", "setCenterCropImg error:$e")
        }
    }
    fun setFxtXYImg(context: Context, url: String?, iv: ImageView, duration: Int = 300, errorResInt: Int = 0, placeResInt: Int = 0) {
        try {
            var options = RequestOptions().priority(Priority.HIGH) //优先级
                .error(errorResInt) //加载失败显示图片
                .placeholder(placeResInt)
                //.transform(MultiTransformation(Fit()/*, GlideRoundTransform(raid)*/))
            CommUtils.println("ImageManger:", "${formatUrl(url)}}")
            Glide.with(context)
                .load(formatUrl(url))
                .apply(options)
                .transition(DrawableTransitionOptions.withCrossFade(duration))
                .into(iv)
        } catch (e: Exception) {
            CommUtils.println("ImageManager->", "setCenterCropImg error:$e")
        }
    }

    fun setBgImageCenterCrop(context: Context, url: String?, iv: ImageView) {
        var options = RequestOptions().priority(Priority.HIGH) //优先级
            .transform(MultiTransformation(CenterCrop()/*, GlideRoundTransform(raid)*/))
        CommUtils.println("ImageManger:", "setBgImage::${formatUrl(url)}")
        Glide.with(context)
            .load(url)
            .apply(options)
            .into(iv)
    }

    /*设置白色 logo*/
    fun setLogWhite(context: Context, iv: ImageView, url: String? = Urls.LOGO_URL_WHITE) {
        var options = RequestOptions().priority(Priority.HIGH) //优先级
            .transform(MultiTransformation(FitCenter()))
        Glide.with(context)
            .load(formatUrl(url))
            .apply(options)
            .into(iv)
    }

    /*设置app logo*/
    fun setLogoApp(context: Context, iv: ImageView, url: String? = Urls.LOGO_URL_APP) {
        var options = RequestOptions().priority(Priority.HIGH) //优先级
            .transform(MultiTransformation(FitCenter()))
        Glide.with(context)
            .load(formatUrl(url))
            .apply(options)
            .into(iv)
    }

    /*设置蓝色 logo*/
    fun setLogoBlue(context: Context, iv: ImageView, url: String? = Urls.LOGO_URL_BLUE) {
        var options = RequestOptions().priority(Priority.HIGH) //优先级
            .transform(MultiTransformation(FitCenter()))
        Glide.with(context)
            .load(formatUrl(url))
            .apply(options)
            .into(iv)
    }

    private fun formatUrl(url: String?): String? {
        var newUrl = if (url?.contains("http://") != false || url.contains("https://")) url else Urls.BaseImgUrl + url
        //if (newUrl?.contains(".jpg") != false) newUrl else newUrl.plus(".jpg")
        CommUtils.println("formatUrl::", "$newUrl")
        return newUrl
    }
}
